package com.aplink.generic.xmlparser;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.aplink.generic.constant.FormDesignConstant;
import com.aplink.generic.constant.MenuConstant;
import com.aplink.generic.pojo.controlspec.menu.MenuItemSpec;
import com.aplink.generic.pojo.controlspec.menu.PopupMenuSpec;

public class PopupMenuParser {

	public static PopupMenuSpec parsePopupMenuFormat(final Node node) {
		final Element elementFormat = (Element) node;
		if (elementFormat.hasChildNodes()) {
			final PopupMenuSpec popupMenuFormat = new PopupMenuSpec();
			NodeList nodeListFormat = elementFormat
					.getElementsByTagName(MenuConstant.POPUP_MENU);
			final Node nodeFormat = nodeListFormat.item(0);
			if (nodeFormat.hasAttributes()) {
				final String controlId = XmlPaserUtil.getAttrValue(nodeFormat,
						FormDesignConstant.ID);
				popupMenuFormat.setControlId(controlId);
			}
			if (nodeFormat.getNodeType() == Node.ELEMENT_NODE) {
				nodeListFormat = ((Element) nodeFormat)
						.getElementsByTagName(MenuConstant.SUB_MENU);
				for (int i = 0; i < nodeListFormat.getLength(); i++) {
					final Node nodeItem = nodeListFormat.item(i);
					final MenuItemSpec popupMenuItem = MenuItemParser
							.parseMenuItemSpec(nodeItem);
					popupMenuFormat.add(popupMenuItem);
				}
			}
			return popupMenuFormat;
		}
		return null;
	}
}
